package com.hdu.pyl.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.hdu.pyl.model.ResultEnum;
import com.hdu.pyl.pojo.News;
import com.hdu.pyl.pojo.User;
import com.hdu.pyl.service.basic.UserService;
import com.hdu.pyl.service.coldBoot.NewsColdBootServiceImpl;
import com.hdu.pyl.service.coldBoot.UserColdBootServiceImpl;
import com.hdu.pyl.service.hot.HotServiceImpl;
import com.hdu.pyl.service.similarity.UserNewsCosineSimilarityImpl;
import com.hdu.pyl.service.tags.TagsRecommenderServiceImpl;
import com.hdu.pyl.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@RestController
@RequestMapping(value = "/getNews")
public class GetNewsController {
    /**
     * 用户是否冷启动
     */
    @Autowired
    private UserColdBootServiceImpl userColdBootService;
    /**
     * 文章冷启动
     */
    @Autowired
    private NewsColdBootServiceImpl newsColdBootService;
    /**
     * 推热门文章
     */
    @Autowired
    private HotServiceImpl hotService;
    /**
     * 根据标签推荐
     */
    @Autowired
    private TagsRecommenderServiceImpl tagsRecommenderService;
    /**
     *  基于用户相似性的推荐算法推荐的  基于余弦相似度的评估
     */
    @Autowired
    private UserNewsCosineSimilarityImpl userNewsCosineSimilarity;

    private boolean isUserColdBoot=false;
    private boolean hasUserTags=false;

    /**
     * 用户信息
     */
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Result<Set<News>> getNews(Integer userId) {
        if (userId == null) {
            return new Result<>(ResultEnum.FAIL.getState(), ResultEnum.FAIL.getInfo(), null);
        }
        User user = userService.findUserById(userId);
        if (user == null) {
            return new Result<>(ResultEnum.FAIL.getState(), ResultEnum.FAIL.getInfo(), null);
        }
        //用户是否冷启动
        isUserColdBoot = userColdBootService.isUserColdBoot(user.getId());
        //用户是否有标签
        hasUserTags = userColdBootService.hasUserTags(user.getId());
        DateTime dateTime = DateUtil.offsetDay(new Date(), -100);
        Set<News> recommenderNews = new HashSet<>();
        if (isUserColdBoot) {
            //用户冷启动
            if (hasUserTags) {
                //存在标签
                recommenderNews.addAll(tagsRecommenderService.getNews(8, user.getId(), dateTime));
                recommenderNews.addAll(hotService.recommendWithNum(2, dateTime));
            } else {
                //没有标签
                recommenderNews.addAll(hotService.recommendWithNum(10, dateTime));
            }
        } else {
            //不是冷启动
            //热门文章
            recommenderNews.addAll(hotService.recommendWithNum(2, dateTime));
            //基于用户相似度的推荐算法
            recommenderNews.addAll(userNewsCosineSimilarity.recommendNews(5, user.getId()));
            //文章冷启动
            recommenderNews.addAll(newsColdBootService.findNewsColdBoot(3, user.getId()));
        }
        return new Result<Set<News>>(ResultEnum.SUCCESS.getState(), ResultEnum.SUCCESS.getInfo(), recommenderNews);
    }
}
